<?php

require_once("includes/sparqllib.php");
require_once("model/QueryManager.class.php");

class router {
 /*
 * @the registry
 */
 private $registry;
 
 private $queryManager;
 /*
 * @the controller path
 */
 private $path;

 private $args = array();

 public $file;

 public $controller;

 public $action; 


 public $OBSERVATION='http://purl.org/linked-data/cube#Observation';
 public $DIMENSION='http://purl.org/linked-data/cube#DimensionProperty';
 public $MEASURE='http://purl.org/linked-data/cube#MeasureProperty';
 public $DATASET='http://purl.org/linked-data/cube#DataSet';

 public $CON_PRE_OBSERVATION='uri';
 public $CON_PRE_DIMENSION='dimension';
 public $CON_PRE_MEASURE='measure';
 public $CON_PRE_DATASET='dataset';

 function __construct($registry) {
        $this->registry = $registry;
 }

 /**
 *
 * @set controller directory path
 *
 * @param string $path
 *
 * @return void
 *
 */
 function setPath($path) {

	/*** check if path i sa directory ***/
	if (is_dir($path) == false)
	{
		throw new Exception ('Invalid controller path: `' . $path . '`');
	}
	/*** set the path ***/
 	$this->path = $path;
}


 /**
 *
 * @load the controller
 *
 * @access public
 *
 * @return void
 *
 */
 public function loader()
 {
	/*** check the route ***/
	$this->getController();

	/*** if the file is not there diaf ***/
	if (is_readable($this->file) == false)
	{
		$this->file = $this->path.'/error404.php';
                $this->controller = 'error404';
	}

	/*** include the controller ***/
	include $this->file;

	/*** a new controller class instance ***/
	$class = $this->controller . 'Controller';
	$controller = new $class($this->registry,array_merge($_POST,$_GET));

	/*** check if the action is callable ***/
	if (is_callable(array($controller, $this->action)) == false)
	{
		$action = 'index';
	}
	else
	{
		$action = $this->action;
	}
		
	/*** run the action ***/
	$controller->$action();
 }


 /**
 *
 * @get the controller
 *
 * @access private
 *
 * @return void
 *
 */
private function getController() {
	
    $route =NULL;
	if(empty($_GET['rt'])&& empty($_POST['rt'])){
     $route =$this->getControllerType($_GET['endpoint'],$_GET['uri']);
	}
	else
	{
	$route =(empty($_GET['rt'])) ? $_POST['rt'] : $_GET['rt'];
	}

	if (empty($route))
	{
		$route = 'index';
	}
	else
	{
		/*** get the parts of the route ***/
		$parts = explode('/', $route);
		$this->controller = $parts[0];
		if(isset( $parts[1]))
		{
			$this->action = $parts[1];
		}
	}

	if (empty($this->controller))
	{
		$this->controller = 'index';
	}

	/*** Get action ***/
	if (empty($this->action))
	{
		$this->action = 'index';
	}

	/*** set the file path ***/
	$this->file = $this->path .'/'. $this->controller . 'Controller.php';
}


public function getControllerType($endpoint,$uri){

    $res=NUll;
    $this->queryManager=QueryManager::GetInstance();
    $this->queryManager->iniConnection($endpoint);
    $sparql=$this->queryManager->getTypeQuery($uri);
    $result = sparql_query( $sparql ); 
    if( !$result ) { print sparql_errno() . ": " . sparql_error(). "\n"; exit; }
			if(sparql_num_rows( $result)==0){
				
					return NUll;
			}
			else{
			  
				while($row = sparql_fetch_array( $result )){
				$res=$row['type'];
				
					if(strcmp($res,$this->OBSERVATION)==0){
					  return $this->CON_PRE_OBSERVATION;
					}
					else
					if(strcmp($res,$this->DIMENSION)==0){
					   return $this->CON_PRE_DIMENSION;
					}	
					else
					if(strcmp($res,$this->MEASURE)==0){
					  return $this->CON_PRE_MEASURE;
					}
					else
					if(strcmp($res,$this->DATASET)==0){
					return $this->CON_PRE_DATASET;
					}
				}
			}
			


}

}

?>
